﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.IO;

namespace ScenarioFramework.Portal
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sDirectory = MapPath("~/");
            DirectoryInfo da = new DirectoryInfo(sDirectory);
            DirectoryInfo[] di = da.GetDirectories();
            FileInfo[] fi = da.GetFiles();

            DataTable dt = new DataTable();
            dt.Columns.Add( new DataColumn("Name", System.Type.GetType("System.String")));
            dt.Columns.Add( new DataColumn("IsFolder", System.Type.GetType("System.Boolean")));
            dt.Columns.Add(new DataColumn("FileExtension", System.Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("Attr", System.Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("Size", System.Type.GetType("System.Int64")));
            dt.Columns.Add(new DataColumn("Modified", System.Type.GetType("System.DateTime")));
            dt.Columns.Add(new DataColumn("Created", System.Type.GetType("System.DateTime")));

            dt.BeginLoadData();
            foreach ( DirectoryInfo d in di )
            {
                AddRowToFileInfoTable(d, dt);
            }
            
            foreach (FileInfo f in fi)
            {
                AddRowToFileInfoTable(f, dt);
            }
            dt.EndLoadData();
            dt.AcceptChanges();

            DataView dv  = new DataView (dt);

            this.GridView1.DataSource = dv;


        }

        private void AddRowToFileInfoTable(FileSystemInfo fi, DataTable dt)
        {
            DataRow dr = dt.NewRow();
            string Attr = AttribString(fi.Attributes);
            dr["Name"] = fi.Name;
            dr["FileExtension"] = Path.GetExtension(fi.Name);
            dr["Attr"] = Attr;
            if  ( Attr.IndexOf("d") > -1 )
            {
                dr["IsFolder"] = true;
                dr["Size"] = 0;
            }
            else {
                dr["IsFolder"] = false;
                dr["Size"] = new FileInfo(fi.FullName).Length;
            }
            
            dr["Modified"] = fi.LastWriteTime;
            dr["Created"] = fi.CreationTime;

            dt.Rows.Add(dr);
        }

        private string AttribString(FileAttributes a)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            if ( (a & FileAttributes.ReadOnly) > 0) sb.Append("r");
            if ( (a & FileAttributes.Hidden) > 0 ) sb.Append("h");
            if ( (a & FileAttributes.System) > 0 ) sb.Append("s");
            if ( (a & FileAttributes.Directory) > 0 ) sb.Append("d");
            if ( (a & FileAttributes.Archive) > 0 ) sb.Append("a");
            if ( (a & FileAttributes.Compressed) > 0) sb.Append("c");
            
            return sb.ToString();
        }
    }
}
